package com.smile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.commons.constant.TaskStateConstant;
import com.smile.commons.constant.TaskTypeConstant;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.mapper.TaskMapper;
import com.smile.model.domain.FeisuanTask;
import com.smile.model.dto.request.EditTaskStatusRequestDTO;
import com.smile.model.dto.request.GetTaskDetailRequestDTO;
import com.smile.model.dto.request.GetTaskListRequestDTO;
import com.smile.model.response.response.GetTaskDetailResponseDTO;
import com.smile.model.response.response.GetTaskListResponseDTO;
import com.smile.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Override
    public Page<GetTaskListResponseDTO> getTaskList(GetTaskListRequestDTO getTaskListRequestDTO) {
        if (ObjectUtil.isNotNull(getTaskListRequestDTO)) {
            QueryWrapper<FeisuanTask> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(StrUtil.isNotBlank(getTaskListRequestDTO.getTaskName()), "task_name", getTaskListRequestDTO.getTaskName());
            queryWrapper.eq(ObjectUtil.isNotNull(getTaskListRequestDTO.getTaskType()), "task_type", getTaskListRequestDTO.getTaskType());
            queryWrapper.eq(ObjectUtil.isNotNull(getTaskListRequestDTO.getStatus()), "status", getTaskListRequestDTO.getStatus());
            Page<FeisuanTask> feisuanTasks = taskMapper.selectPage(new Page<>(getTaskListRequestDTO.getCurrentPage(), getTaskListRequestDTO.getPageSize()), queryWrapper);
            List<GetTaskListResponseDTO> collect = feisuanTasks.getRecords().stream().map(i -> {
                GetTaskListResponseDTO getTaskListResponseDTO = BeanUtil.copyProperties(i, GetTaskListResponseDTO.class);

                switch (i.getTaskType()) {
                    case TaskTypeConstant.PATROL_INSPECTION_TYPE:
                        getTaskListResponseDTO.setTaskType(TaskTypeConstant.PATROL_INSPECTION_TYPE_VALUE);
                        break;
                    case TaskTypeConstant.POLICE_TYPE:
                        getTaskListResponseDTO.setTaskType(TaskTypeConstant.POLICE_TYPE_VALUE);
                        break;
                    case TaskTypeConstant.AUTO_FLY_TYPE:
                        getTaskListResponseDTO.setTaskType(TaskTypeConstant.AUTO_FLY_TYPE_VALUE);
                        break;
                }

                switch (i.getImportantLevel()) {
                    case 1:
                        getTaskListResponseDTO.setImportantLevel("重要");
                        break;
                    case 2:
                        getTaskListResponseDTO.setImportantLevel("一般");
                }

                switch (i.getStatus()) {
                    case TaskStateConstant.PRE_EXECUTE ->
                            getTaskListResponseDTO.setStatus(TaskStateConstant.PRE_EXECUTE_VALUE);
                    case TaskStateConstant.EXECUTING ->
                            getTaskListResponseDTO.setStatus(TaskStateConstant.EXECUTING_VALUE);
                    case TaskStateConstant.INTERRUPTED ->
                            getTaskListResponseDTO.setStatus(TaskStateConstant.INTERRUPTED_VALUE);
                    case TaskStateConstant.CANCEL -> getTaskListResponseDTO.setStatus(TaskStateConstant.CANCEL_VALUE);
                    case TaskStateConstant.FINISHED ->
                            getTaskListResponseDTO.setStatus(TaskStateConstant.FINISHED_VALUE);

                }

                getTaskListResponseDTO.setAirlineName("大里路警务站巡逻航线");
                getTaskListResponseDTO.setErrCount(0);

//                getTaskListResponseDTO.setTaskType();

                return getTaskListResponseDTO;
            }).collect(Collectors.toList());
            Page<GetTaskListResponseDTO> getTaskListResponseDTOPage = MyBatisPlusPageConverter.convertPage(feisuanTasks, GetTaskListResponseDTO.class);
            getTaskListResponseDTOPage.setRecords(collect);
            return getTaskListResponseDTOPage;

        } else {
            return new Page<>();
        }

//        return null;
    }

    @Override
    public GetTaskDetailResponseDTO getTaskDetail(GetTaskDetailRequestDTO getTaskDetailRequestDTO) {
        FeisuanTask feisuanTask = taskMapper.selectById(getTaskDetailRequestDTO.getId());
        if (ObjectUtil.isNotNull(feisuanTask)) {
            GetTaskDetailResponseDTO getTaskDetailResponseDTO = BeanUtil.copyProperties(feisuanTask, GetTaskDetailResponseDTO.class);

            switch (feisuanTask.getTaskType()) {
                case TaskTypeConstant.PATROL_INSPECTION_TYPE:
                    getTaskDetailResponseDTO.setTaskType(TaskTypeConstant.PATROL_INSPECTION_TYPE_VALUE);
                    break;
                case TaskTypeConstant.POLICE_TYPE:
                    getTaskDetailResponseDTO.setTaskType(TaskTypeConstant.POLICE_TYPE_VALUE);
                    break;

                case TaskTypeConstant.AUTO_FLY_TYPE:
                    getTaskDetailResponseDTO.setTaskType(TaskTypeConstant.AUTO_FLY_TYPE_VALUE);
                    break;
            }



            switch (feisuanTask.getImportantLevel()) {
                case 1:
                    getTaskDetailResponseDTO.setImportantLevel("重要");
                    break;
                case 2:
                    getTaskDetailResponseDTO.setImportantLevel("一般");
            }

            switch (feisuanTask.getStatus()) {
                case TaskStateConstant.PRE_EXECUTE ->
                        getTaskDetailResponseDTO.setStatus(TaskStateConstant.PRE_EXECUTE_VALUE);
                case TaskStateConstant.EXECUTING ->
                        getTaskDetailResponseDTO.setStatus(TaskStateConstant.EXECUTING_VALUE);
                case TaskStateConstant.INTERRUPTED ->
                        getTaskDetailResponseDTO.setStatus(TaskStateConstant.INTERRUPTED_VALUE);
                case TaskStateConstant.CANCEL -> getTaskDetailResponseDTO.setStatus(TaskStateConstant.CANCEL_VALUE);
                case TaskStateConstant.FINISHED -> getTaskDetailResponseDTO.setStatus(TaskStateConstant.FINISHED_VALUE);

            }

            getTaskDetailResponseDTO.setAirlineName("大里路警务站巡逻航线");
//            getTaskDetailResponseDTO.setErrCount(0);
            getTaskDetailResponseDTO.setOnSiteInspectionPeriod("每周一 12:20 至 12:50");
            getTaskDetailResponseDTO.setErrType("人员聚集");
            getTaskDetailResponseDTO.setOnSiteInspectionDistance(2L);
            getTaskDetailResponseDTO.setOnSiteInspectionTimes(20L);
            getTaskDetailResponseDTO.setErrNums(0);

            return getTaskDetailResponseDTO;

        } else {
            throw new RuntimeException("任务不存在");
        }
    }

    @Override
    public Boolean editTaskStatus(EditTaskStatusRequestDTO editTaskStatusRequestDTO) {
        if (ObjectUtil.isNotNull(editTaskStatusRequestDTO)) {
            UpdateWrapper<FeisuanTask> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq(ObjectUtil.isNotNull(editTaskStatusRequestDTO.getId()), "id", editTaskStatusRequestDTO.getId())
                    .set(ObjectUtil.isNotNull(editTaskStatusRequestDTO.getStatus()), "status", editTaskStatusRequestDTO.getStatus());
            taskMapper.update(null, updateWrapper);
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }
}
